package cn.itsub.sodo.util;

import cn.itsub.sodo.core.types.Vec2;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

  
/**
 * Game工具类
 * @author Mr.Xia
 * @version 1.4
 * www.zhenzhigu.com
 */
public class Util {
    //bufferedimage.getColorModel().getTransparency()以获得原始图片透明度用于生成缓存图，在缓存图进行相应操作即可。
       
	
	public static BufferedImage rotateImage(final BufferedImage bufferedimage,final double degree){
		return rotateImage(bufferedimage, (int)degree);
	}
	public static BufferedImage resizeImage(final BufferedImage bufferedimage,final double w, final double h){
		return resizeImage(bufferedimage, (int)w, (int)h);
	}
	
	public static BufferedImage opacity(final BufferedImage bufferedimage,float alpha) {
        int w = bufferedimage.getWidth();
        int h = bufferedimage.getHeight();
        BufferedImage img = new BufferedImage(
        		bufferedimage.getWidth(null),
        		bufferedimage.getHeight(null), BufferedImage.TYPE_INT_ARGB); 
        Graphics2D graphics2d = img.createGraphics();
        graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,alpha));
        graphics2d.drawImage(bufferedimage, 0, 0,w,h, null);
        graphics2d.dispose();
        return img;
    }
	
    	/**
         * 旋转图片为指定角度
         * @param bufferedimage 目标图像
         * @param degree 旋转角度
         * @return
         */
        public static BufferedImage rotateImage(final BufferedImage bufferedimage,
                final int degree) {
            int w = bufferedimage.getWidth();
            int h = bufferedimage.getHeight();
            int q = (int) Math.hypot(w, h);
            int type = bufferedimage.getColorModel().getTransparency();
            BufferedImage img=new BufferedImage(q, q, type);
            Graphics2D graphics2d = img.createGraphics();
            graphics2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2d.rotate(Math.toRadians(degree), q / 2, q / 2);
            graphics2d.drawImage(bufferedimage, (q-w)/2, (q-h)/2, null);
            graphics2d.dispose();
            return img;
        }
       
        /**
         * 变更图像为指定大小
         * @param bufferedimage 目标图像
         * @param w 宽
         * @param h 高
         * @return
         */
        public static BufferedImage resizeImage(final BufferedImage bufferedimage,
                final int w, final int h) {
            int type = bufferedimage.getColorModel().getTransparency();
            BufferedImage img;
            Graphics2D graphics2d;
            (graphics2d = (img = new BufferedImage(w, h, type))
                    .createGraphics()).setRenderingHint(
                    RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            graphics2d.drawImage(bufferedimage, 0, 0, w, h, 0, 0, bufferedimage
                    .getWidth(), bufferedimage.getHeight(), null);
            
            graphics2d.setColor(Color.WHITE);     
            /*graphics2d.drawRect(0, 0, w - 1, h - 1);  
            graphics2d.drawRect(1, 1, w - 1, h - 1);  
            graphics2d.drawRect(0, 0, w - 2, h - 2); */
            
            graphics2d.dispose();
            return img;
        }
       
        /**
         * 水平翻转图像
         * @param bufferedimage 目标图像
         * @return
         */
        public static BufferedImage flipImage(final BufferedImage bufferedimage) {
            int w = bufferedimage.getWidth();
            int h = bufferedimage.getHeight();
            BufferedImage img;
            Graphics2D graphics2d;
            (graphics2d = (img = new BufferedImage(w, h, bufferedimage
                    .getColorModel().getTransparency())).createGraphics())
                    .drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
            graphics2d.dispose();
            return img;
        }
        
        /**
         * 计算两点之间角度
         * @param x1
         * @param y1
         * @param x2
         * @param y2
         * @return
         */
        public static double ppDir(double x1,double y1,double x2,double y2){
        	double len_x = x2 - x1;
        	double len_y = y2 - y1;
        	double radian = Math.atan2(len_y, len_x);//弧度
        	return radian*180/Math.PI;//角度
        	
        }
        
        /**
         * 极坐标位移点
         * @param p
         * @param len
         * @param dir
         * @return
         */
        public static Vec2 nextVec2(Vec2 p, double len, double dir){
        	float x = (float) (p.x+len*Math.cos(dir*Math.PI/180));
        	float y = (float) (p.y+len*Math.sin(dir*Math.PI/180));
        	return new Vec2(x, y);
        }
        
        /**
         * 计算两点间距
         * @param p1
         * @param p2
         * @return
         */
        public static double ppLen(Vec2 p1,Vec2 p2){
        	return Math.hypot(p1.x-p2.x, p1.y-p2.y);
        }
        
        /**
         * 窗口局部坐标转为地图坐标
         * @param source
         * @param local
         * @return
         */
        public static Vec2 l2p(Vec2 source,Vec2 local){
        	float x=local.x-source.x;
        	float y=local.y-source.y;
        	return new Vec2(x, y);
        }
        /**
         * 地图坐标转化为窗口局部坐标
         * @return
         */
        public static Vec2 p2l(Vec2 frameVec2){
        	//Vec2 ps=Data.gamePanel.getVec2();
        	//double x=frameVec2.x+ps.x;
        	//double y=frameVec2.y+ps.y;
        	//return new Vec2(x, y);
        	return null;
        }
        
        /*
         * 指定中心点获取矩形
         */
        public static Rectangle getRect(Vec2 centerVec2,int width,int height){
        	return new Rectangle((int)(centerVec2.x-(width/2)), (int)centerVec2.y-(height/2), (int)width, (int)height);
        }
}